var result;
var stringBundle = null;

function init()
{
    result = window.arguments[0];

    this.stringBundle = Components.classes['@mozilla.org/intl/stringbundle;1'].getService(Components.interfaces.nsIStringBundleService).createBundle("chrome://ellaunch/locale/prefs_addgame.properties");
}

function browseFile()
{
    const filePicker = Components.classes['@mozilla.org/filepicker;1'].createInstance(Components.interfaces.nsIFilePicker);

    filePicker.init(window, this.stringBundle.GetStringFromName("browseExec"), Components.interfaces.nsIFilePicker.modeOpen);
    filePicker.defaultString = "";
    var res = filePicker.show();
    switch (res) {
        case Components.interfaces.nsIFilePicker.returnOK:
            document.getElementById("game_path").value = filePicker.file.path;
            break;
        default:
            break;
    }
}

function saveChanges()
{
    var protocol = document.getElementById("game_protocol").value;
    var name = document.getElementById("game_name").value;
    var path = document.getElementById("game_path").value;
    var args = document.getElementById("game_args").value;

    if (protocol.length < 2 || protocol.length > 6 || !/^[a-zA-Z0-9]+$/.test(protocol)) {
        alert(this.stringBundle.GetStringFromName("invalidProtocol"));
        return false;
    }

    if (name.length < 2) {
        alert(this.stringBundle.GetStringFromName("inputName"));
        return false;
    }

    if (window.opener.GamesTreeView.existsGameProtocol(protocol)) {
        alert(this.stringBundle.GetStringFromName("existsProtocol"));
        return false;
    }

    result.protocol = protocol;
    result.name = name;
    result.path = path;
    result.args = args;

    return true;
}